﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BLToolkit.DataAccess;
using BLToolkit.Data.Linq;
using BLToolkit.Mapping;

namespace Core
{
    [TableName("tblDonVi")]

    public class DonVi
    {

        [PrimaryKey, Identity]
        [MapField("MaDonVi")]
        public int ID { get; set; }
        public string TenDonVi { get; set; }
        [Association(ThisKey = "MaDonVi", OtherKey = "ID", CanBeNull = true)]
        public List<CanBo> CanBos { get; set; }
        public static IQueryable<DonVi> DonVis
        {
            get
            {
                using (var db = new DataContext())
                {
                    return db.GetTable<DonVi>();
                }
            }
        }
        public static List<DonVi> All()
        {
            return DonVis.ToList();
        }
        public static DonVi Single(int id)
        {
            return DonVis.SingleOrDefault(x => x.ID == id);
        }
        public static int Add(DonVi entity)
        {
            using (var db = new DataContext())
            {
                object rs = db.InsertWithIdentity(entity);
                return int.Parse(rs.ToString());
            }
        }
        public static int Update(DonVi entity)
        {
            using (var db = new DataContext())
            {
                return db.Update(entity);
            }
        }
        public static int Delete(int id)
        {
            return DonVis.Delete(x => x.ID == id);
        }
    }
}
